package com.cyjt.operation.anim;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.util.Log;

/**
 * 定义PropertyAnimation的动画类
 *
 * @author kullo<BR>
 *         2014-7-23 下午2:51:37<BR>
 */
public class PropertyAnimation {
	public static final long ANIMATION_DURA_TIME_SHORT = 100;
	public static final long ANIMATION_DURA_TIME_LIT = 200;
	public static final long ANIMATION_DELAY_TIME_NO = 0;
	public static final long ANIMATION_DELAY_TIME_SHORT = 10;
	public static final long ANIMATION_DELAY_TIME_LIT = 20;
	private Context context;
	private ObjectAnimator oA;

	public static PropertyAnimation instanceAnimation(Context context) {
		PropertyAnimation fragment = new PropertyAnimation(context);

		return fragment;
	}

	public PropertyAnimation(Context context) {
		this.context = context;
	}

	/**
	 * @param needAnimationView
	 * @param delay
	 * @param dura
	 * @param form
	 * @param to
	 * @return
	 */
	public ObjectAnimator xMoveAnimate(Object needAnimationView, long delay, long dura, float form, float to) {
		oA = new ObjectAnimator();
		oA.setFloatValues(form, to);
		oA.setDuration(dura);
		oA.setPropertyName("x");
		oA.setStartDelay(delay);
		oA.setTarget(needAnimationView);
		Log.v("demo", "[form, to]:" + "[" + form + "," + to + "]");
		return oA;
	}

	public ObjectAnimator rotationAnimate(Object needAnimationView, long delay, long dura, float formDegree,
			float toDegree) {
		oA = new ObjectAnimator();
		oA.setFloatValues(formDegree, toDegree);
		oA.setDuration(dura);
		oA.setPropertyName("rotation");
		oA.setStartDelay(delay);
		oA.setTarget(needAnimationView);
		Log.v("demo", "[form, to]:" + "[" + formDegree + "," + toDegree + "]");
		return oA;
	}

	public ObjectAnimator yMoveAnimate(Object needAnimationView, long delay, long dura, float form, float to) {
		oA = new ObjectAnimator();
		oA.setFloatValues(form, to);
		oA.setDuration(dura);
		oA.setPropertyName("y");
		oA.setStartDelay(delay);
		oA.setTarget(needAnimationView);
		return oA;
	}

	/**
	 * 缩放动画
	 *
	 * @param needAnimationView
	 * @param delay
	 * @param dura
	 * @param form
	 * @param to
	 * @param keyFrameCount
	 * @return
	 */
	public ObjectAnimator scaleAnimate(Object needAnimationView, long delay, long dura, float form, float to,
			int keyFrameCount) {
		Keyframe[] keyframesX = new Keyframe[keyFrameCount + 1];
		Keyframe[] keyframesY = new Keyframe[keyFrameCount + 1];
		float degree = (to - form) / (float) keyFrameCount;
		float timer = 1 / (float) keyFrameCount;
		for (int i = 0; i < keyFrameCount + 1; i++) {
			keyframesX[i] = Keyframe.ofFloat(timer * i, form + degree * i);
			keyframesY[i] = Keyframe.ofFloat(timer * i, form + degree * i);
		}
		PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("scaleX", keyframesX);
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe("scaleY", keyframesY);
		oA = ObjectAnimator.ofPropertyValuesHolder(needAnimationView, pvhX, pvhY);
		oA.setDuration(dura);
		oA.setStartDelay(delay);
		oA.setTarget(needAnimationView);
		Log.v("demo", "[form, to]:" + "[" + form + "," + to + "]");
		return oA;
	}

	/**
	 * 按圆旋转动画
	 *
	 * @param needAnimationView
	 *            要旋转的控件
	 * @param delay
	 *            延时
	 * @param dura
	 *            动画时间
	 * @param xAnchor
	 *            旋转轴X坐标
	 * @param yAnchor
	 *            旋转轴Y坐标
	 * @param radius
	 *            旋转半径
	 * @param formDegree
	 *            起始角度
	 * @param toDegree
	 *            结束角度
	 * @param keyFrameCount
	 *            关键帧数量
	 * @return
	 */
	public ObjectAnimator parkingButtonAnimate(Object needAnimationView, long delay, long dura, float xAnchor,
			float yAnchor, float radius, float formDegree, float toDegree, int keyFrameCount) {
		Keyframe[] keyframesX = new Keyframe[keyFrameCount + 1];
		Keyframe[] keyframesY = new Keyframe[keyFrameCount + 1];
		float degree = (toDegree - formDegree) / (float) keyFrameCount;
		float timer = 1 / (float) keyFrameCount;
		for (int i = 0; i < keyFrameCount + 1; i++) {
			keyframesX[i] = Keyframe.ofFloat(timer * i, (float) (xAnchor - radius * Math.sin(degree * i + formDegree)));
			keyframesY[i] = Keyframe.ofFloat(timer * i, (float) (yAnchor - radius * Math.cos(degree * i + formDegree)));
		}
		PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("x", keyframesX);
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe("y", keyframesY);
		oA = ObjectAnimator.ofPropertyValuesHolder(needAnimationView, pvhX, pvhY);
		oA.setDuration(dura);
		oA.setStartDelay(delay);
		return oA;
	}

	/**
	 * 渐变淡入动画
	 *
	 * @param needAnimationView
	 * @param delay
	 * @param dura
	 * @return
	 */
	public ObjectAnimator alphaAnimate(Object needAnimationView, long delay, long dura, float form, float to) {
		oA = new ObjectAnimator();
		oA.setFloatValues(form, to);
		oA.setDuration(dura);
		oA.setPropertyName("alpha");
		oA.setStartDelay(delay);
		oA.setTarget(needAnimationView);
		return oA;
	}
}
